package vn.ikame.android.boojtran.truyenbua.logic;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ParseFileLink {
	public static final String NODE_NAME = "asset";
	public static final String URL_KEY = "url";
	public static final String LINK_SAVE_KEY = "linkSave";
	public static final String CHECK_SUM_KEY = "checkSum";
	public static final String SIZE_KEY = "size";

	public static ArrayList<String> parseFileList(String fileName) {
		ArrayList<String> list = new ArrayList<String>();
		String xml = readFile(fileName);
		if (xml == null)
			return list;
		Document doc = XMLfunctions.XMLfromString(xml);
		NodeList nodes = doc.getElementsByTagName(NODE_NAME);

		for (int i = 0; i < nodes.getLength(); i++) {
			Element e = (Element) nodes.item(i);
			String assetDownload = XMLfunctions.getValue(e, URL_KEY);
			list.add(CConstant.SVR_LINK + assetDownload);
		}
		return list;
	}

	public static String readFile(String fileName) {
		FileInputStream fIn = null;

		byte[] inputBuffer;
		try {
			fIn = new FileInputStream(fileName);
			inputBuffer = new byte[fIn.available()];
			fIn.read(inputBuffer);
			return new String(inputBuffer);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fIn != null)
					fIn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
